package config

import (
	"fmt"
	"gopkg.in/yaml.v2"
	"io/ioutil"
)

type ServerConfig struct {
	Port    int    `yaml:"port"`
	Address string `yaml:"address"`
}

type CommonConfig struct {
	Server ServerConfig `yaml:"server"`
}

type LogConfig struct {
	Log LogsConfig `yaml:"logs"`
}
type LogsConfig struct {
	Level      int    `yaml:"level"`
	Path       string `yaml:"path"`
	MaxSize    int    `yaml:"max-size"`
	MaxBackups int    `yaml:"max-backups" `
	MaxAge     int    `yaml:"max-age" `
	Compress   bool   `yaml:"compress"`
}

type RedisConfig struct {
	CRedis BaseMg `yaml:"redis"`
}
type RabbitMqConfig struct {
	RabbitMq BaseMg `yaml:"redis"`
}
type BaseMg struct {
	Ip       string `yaml:"ip"`
	Port     int    `yaml:"port"`
	UserName string `yaml:"userName"`
	PassWord string `yaml:"passWord"`
}

// 对外提供服务的端口
func QueryConfig() ServerConfig {
	// 读取YAML文件
	data, err := ioutil.ReadFile("application.yml")
	if err != nil {
		panic(err)
	}
	// 解析YAML到Config结构体
	var config CommonConfig
	err = yaml.Unmarshal(data, &config)
	if err != nil {
		panic(err)
	}
	fmt.Println(config.Server.Port)
	fmt.Println(config.Server.Address)
	return config.Server
}

// redis配置
func QueryRedisConfig() BaseMg {
	// 读取YAML文件
	data, err := ioutil.ReadFile("application.yml")
	if err != nil {
		panic(err)
	}
	// 解析YAML到Config结构体
	var redisConfig RedisConfig
	err = yaml.Unmarshal(data, &redisConfig)
	if err != nil {
		panic(err)
	}
	return redisConfig.CRedis
}

// redis配置
func QueryLogConfig() LogsConfig {
	// 读取YAML文件
	data, err := ioutil.ReadFile("application.yml")
	if err != nil {
		panic(err)
	}
	// 解析YAML到Config结构体
	var logConfig LogConfig
	err = yaml.Unmarshal(data, &logConfig)
	if err != nil {
		panic(err)
	}
	return logConfig.Log
}
